<?php
namespace cmf\controller;
use cmf\controller\PluginRestBaseController;
use cmf\lib\interfaceauth\InterfaceAuth;
use think\Config;


//插件接口控制Api器基类
class ApiThirdAccessBaseController extends PluginRestBaseController
{
    protected $param ; 
    // 获取token 
    public function _initialize()
    {
        parent::_initialize();
        $this->param = $this->request->param();
        $this->start();
    }
    
    /**
     * 开始方法
     **/
    private function start()
    {
        if( !isset( $this->param[ 'appid' ] ) || empty( $this->param [ 'appid' ] ) ){
          
           return api_json_echo( false , 'appid不存在' , null , 20001 );
        }
        if( !isset( $this->param[ 'key' ] ) || empty( $this->param [ 'key' ] ) ){
           
           return api_json_echo( false , 'key不存在' , null , 20003 );
        }
        
        if( $this->param[ 'appid' ] !== Config::get('sign_config.appid')  ){
           return api_json_echo( false , '无效的appid' , null , 20004 );
        }
        
        if( $this->param[ 'key' ] !== Config::get('sign_config.appsecret')  ){
           return api_json_echo( false , '无效的key' , null , 20005 );       
        }
        
    }
    
    /**
     * 设置标识信息
     * @param $uid 用户标识
     * @param $other 其他信息
     **/
    protected function setIdentity( $uid = '' , $other='' ){
        $data = [
            'uid' => $uid,
            'other' => $other
        ];
        return InterfaceAuth::createToken( $data );
    }
    
    /**
     * 设置刷新token
     * @param $uid 用户标识
     * @param $other 其他信息
     * @param $expired 过期时间(秒) 默认:30天 24*60*60*30=2592000秒
    **/
    protected function setRefreshToken( $uid , $other='' , $expired = '2592000' ){
        $data = [
            'uid' => $uid,
            'refresh_token'=>'20200707',
            'other' => $other
        ];
        return InterfaceAuth::createToken( $data , 'REFRESH_TOKEN' , $expired );
    }
    

}